package com.taptech.doufu.ui.view.topicview.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.novel.VipMixWorksBean;
import com.taptech.doufu.ui.view.topicview.UmengEventUtil;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class VipPrivitegeHorizontalAdapter extends RecyclerView.Adapter<VipPrivitegeHorizontalAdapter.ViewHolder>{

    private List<VipMixWorksBean> list;
    private Context context;
    private EventBean eventBean;
    public VipPrivitegeHorizontalAdapter(Context context, List<VipMixWorksBean> list,EventBean eventBean){
        this.list = list;
        this.context = context;
        this.eventBean = eventBean;
    }

    public VipMixWorksBean getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }

    public void refrush(List<VipMixWorksBean> list) {
        if (list != null) {
            this.list = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.vip_privitege_horizontal_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.title = (TextView) view.findViewById(R.id.title);
        holder.icon = (ImageView) view.findViewById(R.id.icon);
        holder.lay = view.findViewById(R.id.lay);
        holder.leftView = view.findViewById(R.id.leftView);
        holder.rightView = view.findViewById(R.id.rightView);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final VipMixWorksBean model = getItem(position);
        if (model != null) {
            holder.title.setText(model.getTitle());
            if (!TextUtils.isEmpty(model.getIcon())) {
                GlideUtil.displayRoundImage(context,holder.icon,model.getIcon(),3);
            }
            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UmengEventUtil.addEvent(context,UmengEventName.VIP_CARD_CONTENT,eventBean);
                    SimpleWeexActivity.startActivity(context,"vue/personalcenter/TFMemberPrivilege.js");
                }
            });
            if (position == 0) {
                holder.leftView.setVisibility(View.VISIBLE);
            }else{
                holder.leftView.setVisibility(View.GONE);
            }

            if (position >0 &&position == getItemCount() - 1) {
                holder.rightView.setVisibility(View.VISIBLE);
            }else{
                holder.rightView.setVisibility(View.GONE);
            }

            changeDayModelBg(holder);
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView icon;
        View lay,leftView,rightView;

        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_color_22));
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.title_text_dark));
    }
}
